<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%
    JSONObject jsonData = new JSONObject(true);
    BaseBean baseBean = new BaseBean();
    String rlzytj = Util.null2String(request.getParameter("rlzytj"));
    String[] strArr = rlzytj.split("~");
    List<String> resourceList = new ArrayList<>();
    List<String> subcompanyList = new ArrayList<>();
    List<String> departmentList = new ArrayList<>();
    List<String> roleList = new ArrayList<>();
    List<String> allresourceList = new ArrayList<>();
    List<String> jobtitleList = new ArrayList<>();
    for (String str : strArr) {
        switch (str.charAt(0)) {
            case '1':
                resourceList.add(str);
                break;
            case '2':
                subcompanyList.add(str);
                break;
            case '3':
                departmentList.add(str);
                break;
            case '4':
                roleList.add(str);
                break;
            case '5':
                allresourceList.add(str);
                break;
            case '6':
                jobtitleList.add(str);
                break;
            default:
        }
    }
    baseBean.writeLog(">>>>>getTjResourceId>>>>>resourceList=" + resourceList);
    baseBean.writeLog(">>>>>getTjResourceId>>>>>subcompanyList=" + subcompanyList);
    baseBean.writeLog(">>>>>getTjResourceId>>>>>departmentList=" + departmentList);
    baseBean.writeLog(">>>>>getTjResourceId>>>>>roleList=" + roleList);
    baseBean.writeLog(">>>>>getTjResourceId>>>>>allresourceList=" + allresourceList);
    baseBean.writeLog(">>>>>getTjResourceId>>>>>jobtitleList=" + jobtitleList);
    try {
        String value = "";
        JSONArray resourceidsList = new JSONArray();
        if (resourceList.size() > 0) {
            JSONObject resourceJson = getResourceidById(resourceList);
            String resourceids = resourceJson.getString("resourceids");
            JSONArray resourceidList = resourceJson.getJSONArray("resourceidList");
            if (resourceidList.size() > 0) {
                value += resourceids;
                resourceidsList.addAll(resourceidList);
            }

        }
        if (subcompanyList.size() > 0) {
            JSONObject subcompanyJson = getResourceidBySubcompany(subcompanyList);
            String resourceids = subcompanyJson.getString("resourceids");
            JSONArray resourceidList = subcompanyJson.getJSONArray("resourceidList");
            if (resourceidList.size() > 0) {
                value += "".equals(value) ? resourceids : "," + resourceids;
                resourceidsList.addAll(resourceidList);
            }
        }
        if (departmentList.size() > 0) {
            JSONObject departmentJson = getResourceidByDepartment(departmentList);
            String resourceids = departmentJson.getString("resourceids");
            JSONArray resourceidList = departmentJson.getJSONArray("resourceidList");
            if (resourceidList.size() > 0) {
                value += "".equals(value) ? resourceids : "," + resourceids;
                resourceidsList.addAll(resourceidList);
            }
        }
        if (roleList.size() > 0) {
            JSONObject roleJson = getResourceidByRole(roleList);
            String resourceids = roleJson.getString("resourceids");
            JSONArray resourceidList = roleJson.getJSONArray("resourceidList");
            if (resourceidList.size() > 0) {
                value += "".equals(value) ? resourceids : "," + resourceids;
                resourceidsList.addAll(resourceidList);
            }
        }
        if (allresourceList.size() > 0) {
            JSONObject allresourceJson = getResourceidByAll(allresourceList);
            String resourceids = allresourceJson.getString("resourceids");
            JSONArray resourceidList = allresourceJson.getJSONArray("resourceidList");
            if (resourceidList.size() > 0) {
                value += "".equals(value) ? resourceids : "," + resourceids;
                resourceidsList.addAll(resourceidList);
            }
        }
        if (jobtitleList.size() > 0) {
            JSONObject jobtitleJson = getResourceidByJobtitle(jobtitleList);
            String resourceids = jobtitleJson.getString("resourceids");
            JSONArray resourceidList = jobtitleJson.getJSONArray("resourceidList");
            if (resourceidList.size() > 0) {
                value += "".equals(value) ? resourceids : "," + resourceids;
                resourceidsList.addAll(resourceidList);
            }
        }
        jsonData.put("value", value);
        jsonData.put("specialobj", resourceidsList);
    } catch (Exception e) {
        e.printStackTrace();
    }
    out.print(jsonData.toJSONString());
%>
<%!
   static BaseBean baseBean = new BaseBean();
    //根据人力资源条件类型为人员获取人力资源id
    public static JSONObject getResourceidById(List<String> list) throws Exception {
        String resourceids = "";
        JSONObject dataJson = new JSONObject(true);
        JSONArray resourceidList = new JSONArray();
        ResourceComInfo comInfo = new ResourceComInfo();
        for (String newStr : list) {
            JSONObject resourceidJson = new JSONObject(true);
            String[] strArr = newStr.split("_");
            resourceids += "".equals(resourceids) ? strArr[1] : "," + strArr[1];
            resourceidJson.put("id", strArr[1]);
            resourceidJson.put("name", comInfo.getLastname(strArr[1]));
            resourceidList.add(resourceidJson);
        }
        dataJson.put("resourceids", resourceids);
        dataJson.put("resourceidList", resourceidList);
        baseBean.writeLog(">>>>>getResourceidById>>>>>dataJson=" + dataJson);
        return dataJson;
    }

    //根据人力资源条件类型为分部获取人力资源id
    public static JSONObject getResourceidBySubcompany(List<String> list) throws Exception {
        String resourceids = "";
        JSONObject dataJson = new JSONObject(true);
        JSONArray resourceidList = new JSONArray();
        ResourceComInfo comInfo = new ResourceComInfo();
        RecordSet rs = new RecordSet();
        for (String str : list) {
            String[] strArr = str.split("\\|@\\|");
            String strArr1 = strArr[0];
            String subcompanyid = strArr1.split("_")[1];
            String minlevel = strArr1.split("_")[3];
            String strArr2 = strArr[1];
            String maxlevel = strArr2.split("_")[0];
            String sql = "select id from hrmresource where subcompanyid1='" + subcompanyid + "' " +
                    "and (seclevel>='" + minlevel + "' and seclevel<='" + maxlevel + "') " +
                    "and status in (0,1,2,3)";
            baseBean.writeLog(">>>>>getResourceidBySubcompany>>>>>sql=" + sql);
            rs.executeQuery(sql);
            while (rs.next()) {
                JSONObject resourceidJson = new JSONObject(true);
                String resourceid = Util.null2String(rs.getString("id"));
                resourceids += "".equals(resourceids) ? resourceid : "," + resourceid;
                resourceidJson.put("id", resourceid);
                resourceidJson.put("name", comInfo.getLastname(resourceid));
                resourceidList.add(resourceidJson);
            }
        }
        dataJson.put("resourceids", resourceids);
        dataJson.put("resourceidList", resourceidList);
        baseBean.writeLog(">>>>>getResourceidBySubcompany>>>>>dataJson=" + dataJson);
        return dataJson;
    }

    //根据人力资源条件类型为部门获取人力资源id
    public static JSONObject getResourceidByDepartment(List<String> list) throws Exception {
        String resourceids = "";
        JSONObject dataJson = new JSONObject(true);
        JSONArray resourceidList = new JSONArray();
        ResourceComInfo comInfo = new ResourceComInfo();
        RecordSet rs = new RecordSet();
        for (String str : list) {
            String[] strArr = str.split("\\|@\\|");
            String strArr1 = strArr[0];
            String departmentid = strArr1.split("_")[1];
            String minlevel = strArr1.split("_")[3];
            String strArr2 = strArr[1];
            String maxlevel = strArr2.split("_")[0];
            String sql = "select id from hrmresource where departmentid='" + departmentid + "' " +
                    "and (seclevel>='" + minlevel + "' and seclevel<='" + maxlevel + "') " +
                    "and status in (0,1,2,3)";
            baseBean.writeLog(">>>>>getResourceidByDepartment>>>>>sql=" + sql);
            rs.executeQuery(sql);
            while (rs.next()) {
                JSONObject resourceidJson = new JSONObject();
                String resourceid = Util.null2String(rs.getString("id"));
                resourceids += "".equals(resourceids) ? resourceid : "," + resourceid;
                resourceidJson.put("id", resourceid);
                resourceidJson.put("name", comInfo.getLastname(resourceid));
                resourceidList.add(resourceidJson);
            }
        }
        dataJson.put("resourceids", resourceids);
        dataJson.put("resourceidList", resourceidList);
        baseBean.writeLog(">>>>>getResourceidByDepartment>>>>>dataJson=" + dataJson);
        return dataJson;
    }

    //根据人力资源条件类型为角色获取人力资源id
    public static JSONObject getResourceidByRole(List<String> list) throws Exception {
        String resourceids = "";
        JSONObject dataJson = new JSONObject(true);
        JSONArray resourceidList = new JSONArray();
        ResourceComInfo comInfo = new ResourceComInfo();
        RecordSet rs = new RecordSet();
        for (String str : list) {
            String[] strArr = str.split("\\|@\\|");
            String strArr1 = strArr[0];
            String roleid = strArr1.split("_")[1];
            String minlevel = strArr1.split("_")[3];
            String strArr2 = strArr[1];
            String maxlevel = strArr2.split("_")[0];
            String sql = "select b.id from hrmrolemembers a join hrmresource b on a.resourceid=b.id " +
                    "where a.roleid='" + roleid + "' " +
                    "and (b.seclevel>='" + minlevel + "' and b.seclevel<='" + maxlevel + "') " +
                    "and b.status in (0,1,2,3)";
            baseBean.writeLog(">>>>>getResourceidByRole>>>>>sql=" + sql);
            rs.executeQuery(sql);
            while (rs.next()) {
                JSONObject resourceidJson = new JSONObject();
                String resourceid = Util.null2String(rs.getString("id"));
                resourceids += "".equals(resourceids) ? resourceid : "," + resourceid;
                resourceidJson.put("id", resourceid);
                resourceidJson.put("name", comInfo.getLastname(resourceid));
                resourceidList.add(resourceidJson);
            }
        }
        dataJson.put("resourceids", resourceids);
        dataJson.put("resourceidList", resourceidList);
        baseBean.writeLog(">>>>>getResourceidByRole>>>>>dataJson=" + dataJson);
        return dataJson;
    }

    //根据人力资源条件类型为所有人获取人力资源id
    public static JSONObject getResourceidByAll(List<String> list) throws Exception {
        String resourceids = "";
        JSONObject dataJson = new JSONObject(true);
        JSONArray resourceidList = new JSONArray();
        ResourceComInfo comInfo = new ResourceComInfo();
        for (String str : list) {
            String[] strArr = str.split("\\|@\\|");
            String minlevel = strArr[0].split("_")[3];
            String maxlevel = strArr[1].split("_")[0];
            String sql = "select id from hrmresource where (seclevel>='" + minlevel + "' and seclevel<='" + maxlevel + "') " +
                    "and status in (0,1,2,3)";
            baseBean.writeLog(">>>>>getResourceidByAll>>>>>sql=" + sql);
            RecordSet rs = new RecordSet();
            rs.executeQuery(sql);
            while (rs.next()) {
                JSONObject resourceidJson = new JSONObject(true);
                String resourceid = Util.null2String(rs.getString("id"));
                resourceids += "".equals(resourceids) ? resourceid : "," + resourceid;
                resourceidJson.put("id", resourceid);
                resourceidJson.put("name", comInfo.getLastname(resourceid));
                resourceidList.add(resourceidJson);
            }
        }
        dataJson.put("resourceids", resourceids);
        dataJson.put("resourceidList", resourceidList);
        baseBean.writeLog(">>>>>getResourceidByAll>>>>>dataJson=" + dataJson);
        return dataJson;

    }

    //根据人力资源条件类型为岗位获取人力资源id
    public static JSONObject getResourceidByJobtitle(List<String> list) throws Exception {
        String resourceids = "";
        JSONObject dataJson = new JSONObject(true);
        JSONArray resourceidList = new JSONArray();
        ResourceComInfo comInfo = new ResourceComInfo();
        String jobtitleids = "";
        for (String str : list) {
            String[] strArr = str.split("\\|@\\|");
            String strArr1 = strArr[0];
            String jobtitleid = strArr1.split("_")[1];
            jobtitleids += "".equals(jobtitleids) ? jobtitleid : "," + jobtitleid;
        }
        String sql = "select id from hrmresource where jobtitle in (" + jobtitleids + ") and status in (0,1,2,3)";
        baseBean.writeLog(">>>>>getResourceidByJobtitle>>>>>sql=" + sql);
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        while (rs.next()) {
            JSONObject resourceidJson = new JSONObject(true);
            String resourceid = Util.null2String(rs.getString("id"));
            resourceids += "".equals(resourceids) ? resourceid : "," + resourceid;
            resourceidJson.put("id", resourceid);
            resourceidJson.put("name", comInfo.getLastname(resourceid));
            resourceidList.add(resourceidJson);
        }
        dataJson.put("resourceids", resourceids);
        dataJson.put("resourceidList", resourceidList);
        baseBean.writeLog(">>>>>getResourceidByJobtitle>>>>>dataJson=" + dataJson);
        return dataJson;
    }

%>